﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace University.Workshop.DataAccess
{
    public class DataAccessSquare
    {
        public static List<string> GetAllSquares()
        {
            WFPEntities.Instance.DiscardChanges();
            return (from v in WFPEntities.Instance.Square
                    select v.SquareId).ToList();
        }

        public static ICollection<Model.Square> GetSquares()
        {
            WFPEntities.Instance.DiscardChanges();
            return (from v in WFPEntities.Instance.Square
                    select v).ToList();
        }

        public static List<Model.Allotment> SearchSquareNotHaveKardexInSquare(Model.Square square)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from v in WFPEntities.Instance.Allotment
                    where v.KardexId == null && v.SquareId == square.SquareId
                    && v.Enable
                    select v).ToList();
        }

        public static List<Model.Allotment> SearchSquareNotHaveKardexInSquareExceptKardex(Model.Square square, Model.Kardex kardex)
        {
            WFPEntities.Instance.DiscardChanges();
            Model.Allotment a = kardex.AllotmentModel;
            var q = (from v in WFPEntities.Instance.Allotment
                     where (v.KardexId == null && v.SquareId == square.SquareId) ||
                           (
                               (v.SquareId == a.SquareId) &&
                               (v.AllotmentId == a.AllotmentId)
                           )&&v.Enable
                     select v);
            return q.ToList();
        }

        public static List<Model.Square> SearchSquareById(Model.Square mp)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from v in WFPEntities.Instance.Square
                    where v.SquareId == mp.SquareId
                    select v).ToList();
        }

        public static void InsertNewSquare(Model.Square newSquare)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.Square.Add(newSquare);
            WFPEntities.Instance.SaveChanges();
        }

        public static bool ExistImage(string ubicationCroquisSquare)
        {
            WFPEntities.Instance.DiscardChanges();
            return File.Exists(ubicationCroquisSquare);
        }

        public static void CopyImageToLocalFolder(string ubicationCroquisSquare, Model.Square SquareRegister)
        {
            if (!System.IO.Directory.Exists(Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format("Images Square"))))
            {
                System.IO.Directory.CreateDirectory(Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format("Images Square")));
            }
            File.Copy(ubicationCroquisSquare,
                      Path.Combine(System.IO.Directory.GetCurrentDirectory(),
                                   string.Format("Images Square\\{0}.jpg",
                                                 SquareRegister.SquareId,
                                                 ubicationCroquisSquare.Split('.').Last())));
        }

        public static ICollection<Model.Square> SearchSquaresByRelativeId(Model.Square mp)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from v in WFPEntities.Instance.Square
                    where v.SquareId.Contains(mp.SquareId)
                    select v).ToList();
        }

        public static string GetUbicationImageSquare(Model.Square square)
        {
            return Path.Combine(System.IO.Directory.GetCurrentDirectory(), string.Format("Images Square\\{0}.jpg", square.SquareId));
        }

        public static List<Model.Square> SearchSquaresWithAllotmentsNotAssignedAndIsActive()
        {

            WFPEntities.Instance.DiscardChanges();
            return (from v in WFPEntities.Instance.Square
                    where v.Enable == true 
                    select v).ToList();
        }

        public static ICollection<Model.Square> SearchSquareByParameterAll(Model.Square k)
        {
            try
            {
                var q = from s in WFPEntities.Instance.Square
                        where s.SquareId.Contains(k.SquareId)
                        select s;
                return q.ToList();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from s in WFPEntities.Instance.Square
                        where s.SquareId.Contains(k.SquareId)
                        select s;
                return q.ToList();
            }
        }

        public static ICollection<Model.Square> SearchSquareByParameterDisable(Model.Square k)
        {
            try
            {
                var q = from s in WFPEntities.Instance.Square
                        where s.SquareId.Contains(k.SquareId)&& (!s.Enable)
                        select s;
                return q.ToList();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from s in WFPEntities.Instance.Square
                        where s.SquareId.Contains(k.SquareId) && (!s.Enable)
                        select s;
                return q.ToList();
            }
        }

        public static ICollection<Model.Square> SearchSquareByParameterEnable(Model.Square k)
        {
            try
            {
                var q = from s in WFPEntities.Instance.Square
                        where s.SquareId.Contains(k.SquareId) && (s.Enable)
                        select s;
                return q.ToList();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from s in WFPEntities.Instance.Square
                        where s.SquareId.Contains(k.SquareId) && (s.Enable)
                        select s;
                return q.ToList();
            }
        }

        public static void ChangeState(Model.Square square)
        {
            try
            {
                var q = (from s in WFPEntities.Instance.Square
                         where s.SquareId.Contains(square.SquareId)
                         select s);
                if (q.Any())
                {
                    var p = q.First();
                    p.Enable = !p.Enable;
                    WFPEntities.Instance.SaveChanges();
                }
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                var q = (from s in WFPEntities.Instance.Square
                        where s.SquareId.Contains(square.SquareId) 
                        select s);
                if(q.Any())
                {
                    var p = q.First();
                    p.Enable = !p.Enable;
                    WFPEntities.Instance.SaveChanges();
                }
            }
        }

        public static void UpdateSquare(Model.Square modifySquare)
        {
            try
            {
                var p = (from s in WFPEntities.Instance.Square
                         where s.SquareId==modifySquare.SquareId
                         select s).First();
                    p.Comment = modifySquare.Comment;
                    WFPEntities.Instance.SaveChanges();
                
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                var p = (from s in WFPEntities.Instance.Square
                         where s.SquareId == modifySquare.SquareId
                         select s).First();
                    p.Comment = modifySquare.Comment;
                    WFPEntities.Instance.SaveChanges();
            }
        }

        public static ICollection<string> GetAllSquaresWithKardexOrAvailable(Model.Kardex kardex)
        {
            WFPEntities.Instance.DiscardChanges();
            //var q=
            return (from s in WFPEntities.Instance.Square
                     where (from n in s.Allotments
                            where n.KardexId==null||n.KardexId==kardex.KardexId
                            select s).Any()
                     select s.SquareId).ToList();
            
        }
    }
}
